#include <stdio.h>  
#include <string.h>  
#include <stdbool.h>  
  
#define MAX_PASSWORD_LENGTH 10  
  
typedef enum {  
    ENTER_OLD_PASSWORD = 0,  
    ENTER_NEW_PASSWORD,  
    CONFIRM_NEW_PASSWORD  
} PasswordChangeState;  
  
typedef struct {  
    PasswordChangeState state;  
    char oldPassword[MAX_PASSWORD_LENGTH];  
    char newPassword[MAX_PASSWORD_LENGTH];  
} PasswordContext;  
  
typedef void (*StateFunction)(PasswordContext *ctx, const char *input);  
    
  
void handleEnterOldPassword(PasswordContext *ctx, const char *input) {  
    if (strcmp(input, ctx->oldPassword) == 0) {  
        ctx->state = ENTER_NEW_PASSWORD;  
        printf("Enter new password: ");  
    } else {  
        printf("Old password incorrect. Please try again.\n");       
    }  
}  
  
void handleEnterNewPassword(PasswordContext *ctx, const char *input) {  
    if (strlen(input) >= 4 && strlen(input) <= MAX_PASSWORD_LENGTH) {  
        strcpy(ctx->newPassword, input);  
        ctx->state = CONFIRM_NEW_PASSWORD;  
        printf("Confirm new password: ");  
    } else {  
        printf("Invalid password length. Please try again.\n");  
    }  
}  
  
void handleConfirmNewPassword(PasswordContext *ctx, const char *input) {  
    if (strcmp(input, ctx->newPassword) == 0) {  
        printf("Password changed successfully!\n");  
        ctx->state = ENTER_OLD_PASSWORD;  // Reset to start if needed
		printf("Enter old password: ");
    } else {  
        printf("New passwords do not match. Please try again.\n");  
        ctx->state = ENTER_NEW_PASSWORD;  
        printf("Enter new password: ");  
    }  
}  
  
StateFunction stateFunctions[] = {  
    handleEnterOldPassword,  
    handleEnterNewPassword,  
    handleConfirmNewPassword  
};  
  
int main() {  
    PasswordContext ctx = {ENTER_OLD_PASSWORD, "123456", ""};  
    char input[MAX_PASSWORD_LENGTH + 1];  
	printf("Enter old password: ");
    while (1) { 
	
	fgets(input, sizeof(input), stdin);  
        input[strcspn(input, "\n")] = 0; // Remove newline  
        stateFunctions[ctx.state](&ctx, input);  
    }  
  
    return 0;  
}